#include("layout.html")
#@layout("正在打开微信支付")

#define script()
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script src="/static/components/jquery/jquery.min.js"></script>
<script>
    var invoke = true;
    var interval = setInterval(function () {
        if (invoke){
            $.get("#(CPATH)/pay/query?trx=#(payment.trx_no)", function (result) {
                if (result && result.state == 'ok') {
                    invoke = false;
                    clearInterval(interval);
                    location.href = "#(CPATH)/pay/success/#(payment.trx_no)"
                }
            });
        }
    }, 1000);

    $(document).ready(function () {

        $.ajax({
            url : "#(CPATH)/pay/getWechatOrderInfo",
            type : "post",
            async: true,
            data : {
                trx_no : "#(payment.trx_no)",
                openId : "#(openId)"
            },
            dataType : 'json',
            success : function(data) {
                if (data.code == 0){
                    if (typeof WeixinJSBridge == "undefined"){
                        if( document.addEventListener ){
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(data), false);
                        }else if (document.attachEvent){
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(data));
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(data));
                        }
                    }else{
                        onBridgeReady(data);
                    }
                    return;
                }
            },
            error : function(edata) {
                alert("服务器异常")
            }
        })

    });

    function onBridgeReady(data){
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": data.appId,                 //公众号名称，由商户传入
                "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数
                "nonceStr": data.nonceStr,           //随机串
                "package": data.package,
                "signType": data.signType,           //微信签名方式：
                "paySign": data.sign                 //微信签名
            },
            function(res){
                // 使用以断前端返回,微信团队郑重提示：res.err_msg 将在用户支付成功后返回 ok，但并不保证它绝对可靠。
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    alert("支付成功")
                }
            }
        );
    }


</script>
#end

#define content()
<div class="pay-header">
    正在打开微信支付....
</div>

<div class="row pay-content">

    <div class="col-lg-12 pay-content-info">
        <div class="pay-content-title">
            正在打开微信支付....
        </div>
        <div class="pay-content-money">
            ￥ #number(payment.pay_amount,'0.00') 元
        </div>
    </div>

</div>
#end
